package com.yuncheng.oaApi.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.OaFlowConstant;
import com.yuncheng.oaApi.entity.OaProcessBusiness;
import com.yuncheng.oaApi.entity.QyReservation;
import com.yuncheng.oaApi.entity.OaSpcyJcyFybxd;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.OaProcessBusinessMapper;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 流程业务主表
 * @Author: yuncheng
 * @Date: 2023-10-23
 * @Version: V1.0
 */
@Service
public class OaProcessBusinessServiceImpl extends BaseServiceImpl<OaProcessBusinessMapper, OaProcessBusiness> implements IOaProcessBusinessService {

    @Resource
    @Lazy
    private OaProcessBusinessMapper oaProcessBusinessMapper;

    @Resource
    @Lazy
    private IQyReservationService qyReservationService;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxdService spcyJcyFybxdService;

    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private IOaYcSysOrgService ycSysOrgService;

    @Resource
    @Lazy
    private INkRsHtspjlService nkRsHtspjlService;



    @Override
    public void addProcess(String oaid,String zy,String bt,String lcmbbm,String ywlx){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        OaProcessBusiness oaProcessBusiness = new OaProcessBusiness();

        if (currentUser == null){
            oaProcessBusiness.setFsqr("");
            oaProcessBusiness.setFsqbm("");
            oaProcessBusiness.setFuserid("");
        }else {
            String orgId = ycSysUserService.getUserZbmByUserId(currentUser.getId());
            YcSysOrg ycSysOrg = ycSysOrgService.getById(orgId);


            oaProcessBusiness.setFsqr(currentUser.getName());
            oaProcessBusiness.setFsqbm(ycSysOrg.getOrgName());
            oaProcessBusiness.setFuserid(currentUser.getId());
        }

        if (lcmbbm.equals(OaFlowConstant.NK_BXSQLC)){
            oaProcessBusiness.setFdjh(spcyJcyFybxdService.getById(oaid).getFdjh());
        }else if (lcmbbm.equals(OaFlowConstant.NK_HTSPLC)) {
            oaProcessBusiness.setFdjh(nkRsHtspjlService.getById(oaid).getFhtbh());

        }else {
            oaProcessBusiness.setFdjh("");
        }


        oaProcessBusiness.setId(GetUuIdUtils.ReplaceUuId());
        oaProcessBusiness.setFpid(oaid);
        oaProcessBusiness.setFbt(bt);
        oaProcessBusiness.setFzy(zy);
        oaProcessBusiness.setFsqsj(DateConversionUtils.DateToYMDMM(new Date()));

        oaProcessBusiness.setFlcmb(lcmbbm);
        oaProcessBusiness.setFywlx(ywlx);
        oaProcessBusiness.setFcshdb(OaFlowConstant.FLOW_BLZT_DB);

        oaProcessBusinessMapper.insert(oaProcessBusiness);
    }

    @Override
    public void addProcessYyzx(String oaid,String zy,String bt,String lcmbbm,String ywlx){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        OaProcessBusiness oaProcessBusiness = new OaProcessBusiness();
        QyReservation qyReservation = qyReservationService.getById(oaid);
        if (qyReservation == null){
            throw new BusinessException("没有查询到咨询预约申请，请联系管理员");
        }
        String fsqqymc = qyReservation.getFsqqymc();
        if (currentUser == null){
            oaProcessBusiness.setFsqr(fsqqymc);
            oaProcessBusiness.setFsqbm("");
            oaProcessBusiness.setFuserid("");
        }else {
            oaProcessBusiness.setFsqr(currentUser.getName());
            oaProcessBusiness.setFsqbm(currentUser.getDeptName());
            oaProcessBusiness.setFuserid(currentUser.getId());
        }

        oaProcessBusiness.setId(GetUuIdUtils.ReplaceUuId());
        oaProcessBusiness.setFpid(oaid);
        oaProcessBusiness.setFbt(bt);
        oaProcessBusiness.setFzy(zy);
        oaProcessBusiness.setFsqsj(DateConversionUtils.DateToYMDMM(new Date()));

        oaProcessBusiness.setFlcmb(lcmbbm);
        oaProcessBusiness.setFywlx(ywlx);
        oaProcessBusiness.setFcshdb(OaFlowConstant.FLOW_BLZT_DB);

        oaProcessBusinessMapper.insert(oaProcessBusiness);
    }


    @Override
    public void addProcessBx(String oaid,String zy,String bt,String lcmbbm,String ywlx){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        OaProcessBusiness oaProcessBusiness = new OaProcessBusiness();

        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(oaid);

        if (currentUser == null){
            oaProcessBusiness.setFsqr("");
            oaProcessBusiness.setFsqbm("");
            oaProcessBusiness.setFuserid("");
        }else if (ywlx.equals(OaCommonFlowConstant.NK)){


            oaProcessBusiness.setFsqr(oaSpcyJcyFybxd.getFjbr());
            oaProcessBusiness.setFsqbm(oaSpcyJcyFybxd.getFjbks());
            oaProcessBusiness.setFuserid(oaSpcyJcyFybxd.getFjbrid());
        }else {
            oaProcessBusiness.setFsqr(currentUser.getName());
            oaProcessBusiness.setFsqbm(currentUser.getDeptName());
            oaProcessBusiness.setFuserid(currentUser.getId());

        }
        if (StringUtils.isNotBlank(oaSpcyJcyFybxd.getFbxlx())){
            oaProcessBusiness.setFbxlx(oaSpcyJcyFybxd.getFbxlx());
        }



        if (lcmbbm.equals(OaFlowConstant.NK_BXSQLC)){
            oaProcessBusiness.setFdjh(spcyJcyFybxdService.getById(oaid).getFdjh());
        }else {
            oaProcessBusiness.setFdjh("");
        }


        oaProcessBusiness.setId(GetUuIdUtils.ReplaceUuId());
        oaProcessBusiness.setFpid(oaid);
        oaProcessBusiness.setFbt(bt);
        oaProcessBusiness.setFzy(zy);
        oaProcessBusiness.setFsqsj(DateConversionUtils.DateToYMDMM(new Date()));
        oaProcessBusiness.setFsfkbx("是");
        oaProcessBusiness.setFlcmb(lcmbbm);
        oaProcessBusiness.setFywlx(ywlx);
        oaProcessBusiness.setFcshdb(OaFlowConstant.FLOW_BLZT_DB);

        oaProcessBusinessMapper.insert(oaProcessBusiness);
    }


}
